
import mongoose from 'mongoose'
const AppId = mongoose.model('AppId')

export const CheckAppId = async (ctx) => {
    // 校验APPID
  let promise = new Promise(async (resolve, reject) => {
    const { appid } = ctx.query
    if (typeof appid !== 'undefined' && appid !== '') {
      const appIdData = await AppId.findOne({appId: appid}).exec()
      if (appIdData !== null && appIdData.length !== 0) {
        const { remainCount } = appIdData
        if (remainCount <= 0) {
          reject(new Error(-102))
        } else {
          await AppId.findOneAndUpdate({appId: appid}, {remainCount: remainCount - 1}).exec()
          resolve(true)
        }
      } else {
        reject(new Error(-101))
      }
    } else {
      reject(new Error(-101))
    }
  })
  return promise
}
